import Vue from 'vue'
import Vuex from 'vuex'
import { getBaiduToken }  from '../api/speech'
Vue.use(Vuex)

let lifeData = {};

try {
	// 尝试获取本地是否存在lifeData变量，第一次启动APP时是不存在的
	lifeData = uni.getStorageSync('lifeData');
} catch (e) {

}

// 需要永久存储，且下次APP启动需要取出的，在state中的变量名
let saveStateKeys = ['vuex_user', 'access_token', 'user_info', 'baidu_token'];

// 保存变量到本地存储中
const saveLifeData = function(key, value) {
	// 判断变量名是否在需要存储的数组中
	if (saveStateKeys.indexOf(key) != -1) {
		// 获取本地存储的lifeData对象，将变量添加到对象中
		let tmp = uni.getStorageSync('lifeData');
		// 第一次打开APP，不存在lifeData变量，故放一个{}空对象
		tmp = tmp ? tmp : {};
		tmp[key] = value;
		// 执行这一步后，所有需要存储的变量，都挂载在本地的lifeData对象中
		uni.setStorageSync('lifeData', tmp);
	}
}

const store = new Vuex.Store({
	state: {
		vuex_tabbar: [{
				iconPath: "/static/image/shelf.png",
				selectedIconPath: "/static/image/shelf_active.png",
				text: '书架',
				pagePath: '/pages/shelf/shelf'
			},
			{
				iconPath: "/static/image/home.png",
				selectedIconPath: "/static/image/home_active.png",
				text: '首页',
				pagePath: '/pages/index/index'
			},
			{
				iconPath: "/static/image/library.png",
				selectedIconPath: "/static/image/library_active.png",
				text: '书库',
				pagePath: '/pages/library/library'
			},
			{
				iconPath: "/static/image/user.png",
				selectedIconPath: "/static/image/user_active.png",
				text: '我的',
				pagePath: '/pages/user/user'
			}
		],
		read_theme: [{
				index: 0,
				item: '日间',
				icon: 'day',
				background: '#fff9f2',
				color: '#352c2a',
				tabBackground: '#FFEDD9'
			},
			{
				index: 1,
				item: '夜间',
				icon: 'night',
				background: '#10121a',
				color: '#d2dbff',
				tabBackground: '#0c0e14'
			}
		],
		iStatusBarHeight: uni.getSystemInfoSync().statusBarHeight,
		screenHeight: uni.getSystemInfoSync().screenHeight,
		windowHeight: uni.getSystemInfoSync().windowHeight,
		access_token: lifeData.access_token ? lifeData.access_token : '',
		user_info: lifeData.user_info ? lifeData.user_info : '',
		baidu_token:lifeData.baidu_token ? lifeData.baidu_token : ''
	},
	mutations: {
		$uStore(state, payload) {
			// 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
			let nameArr = payload.name.split('.');
			let saveKey = '';
			let len = nameArr.length;
			if (len >= 2) {
				let obj = state[nameArr[0]];
				for (let i = 1; i < len - 1; i++) {
					obj = obj[nameArr[i]];
				}
				obj[nameArr[len - 1]] = payload.value;
				saveKey = nameArr[0];
			} else {
				// 单层级变量，在state就是一个普通变量的情况
				state[payload.name] = payload.value;
				saveKey = payload.name;
			}
			// 保存变量到本地，见顶部函数定义
			saveLifeData(saveKey, state[saveKey])
		},
		resetToken() {
			store.state.access_token = '';
			store.state.user_info = '';
			saveLifeData('access_token', '');
			saveLifeData('user_info', '')
		}
	},
	actions: {
		resetToken({commit}) {
			return new Promise((resolve, reject) => {
				commit('resetToken');
				resolve('ok')
			})
		},
		getBaiduToken({commit}){
			return new Promise((resolve,reject) =>{
				getBaiduToken().then(res=>{
					console.log(res)
					store.state.baidu_token = res.data;
					saveLifeData('baidu_token', res.data);
					resolve('ok')
				})
			})
		}
	}
})

export default store
